#pragma once
#include <thread>
#include <mutex>
#include "EventLoop.h"
#include "IPoller.h"

// one eventloop per thread
class Poller : public IPoller {
public:

    Poller(): thread_(&Poller::Run, this){

    }

    ~Poller() {
        if (thread_.joinable()) {
            thread_.join();
        }
    }
    
    void AddEvent(int fd, uint32_t events, void* ptr) override {
        evloop_.AddEvent(fd, events, ptr);
    }

    void ModEvent(int fd, uint32_t events, void* ptr) override {
        evloop_.ModEvent(fd, events, ptr);
    }

    void DelEvent(int fd) override {
        evloop_.DelEvent(fd);
    }

    void Run() override {
        evloop_.Run();
    }

private:
    EventLoop evloop_;
    std::thread thread_;
    std::mutex mutex_; // 保护线程安全
};